package com.winning.pmph.schedule;

import java.io.File;
import java.util.Date;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.winning.pmph.entity.IncreaseResource;
import com.winning.pmph.service.IncreaseResourceService;
import com.winning.pmph.utils.ApplicationContextUtil;
import com.winning.pmph.utils.FfmpegUtil;
import com.winning.pmph.utils.ResourceServerUtils;

@Component
public class IncreaseVideoJob implements Job {
	
	protected Log log = LogFactory.getLog(IncreaseVideoJob.class);
	
	@Autowired
	IncreaseResourceService increaseResourceService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		JobDetail jobDetail = context.getJobDetail();
		log.info("压缩任务开始");
		JobKey jobKey = jobDetail.getKey();
		String taskId = jobKey.getName();
		log.info("taskId="+taskId);
		try {
			IncreaseResource resource = increaseResourceService.getById(taskId);
			String path = resource.getPath();
			if (StringUtils.isBlank(path)) {
				return;
			}
			
			String suffix = ResourceServerUtils.getFileUrlSuffix(path);
			int resourceType = FfmpegUtil.getResourceType(suffix);
			if (resourceType != 2) {
				return;
			}
			//下载文件到本地
			String localFilePath = ResourceServerUtils.resourceFileDownload(path);
			if(StringUtils.isBlank(localFilePath)) {
				return;
			}
			log.info("下载到本地完成");
			if (StringUtils.isBlank(resource.getImgPath())) {
				try {
					log.info("截取图片");
					//截取视频首帧
					String destImgPath = localFilePath.substring(0, localFilePath.lastIndexOf(".")) + ".png";
					FfmpegUtil.getThumb(localFilePath, destImgPath, 1280, 720, 0, 0, 1);
					String url = ResourceServerUtils.uploadFile(destImgPath);
					if(StringUtils.isNotBlank(url)) {
						resource.setImgPath(url);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			File file = new File(localFilePath);
			long fileSize = file.length();
			resource.setHdSize(getFileSize(fileSize));
			long sdSize = fileSize / 4;
			resource.setSdSize(getFileSize(sdSize));
			
			String sdLocalPath = ApplicationContextUtil.BASE_DOWN_FORDER + "increaseresource/"+IdWorker.getIdStr()+"_480" + suffix;
			File destPathFile = new File(sdLocalPath);
			log.info("正式压缩命令开始："+destPathFile);
			FfmpegUtil.convertToStandard(localFilePath, sdLocalPath);
			log.info("压缩完成："+sdLocalPath);
			File destFile = new File(sdLocalPath);
			if(destFile.exists()) {
				log.info("上传资源服务器："+sdLocalPath);
				String url = ResourceServerUtils.uploadFile(sdLocalPath);
				log.info("上传资源服务器完成："+url);
				if(StringUtils.isNotBlank(url)) {
					resource.setSdVideoPath(url);
				}
				resource.setSdSize(getFileSize(destPathFile.length()));
			}
			resource.setUpdateTime(new Date());
			increaseResourceService.updateById(resource);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			log.info("finally end ..............");
		}
	}
	
	private String getFileSize(long fileSize) {
		long size = fileSize / 1024 / 1024;
		String sizeStr = size+"MB";
		if(size<1) {
			sizeStr = fileSize / 1024+"KB";
		}
		return sizeStr;
	}
}
